/**
 * Copyright(c) 2013 Jade Techonologies Co., Ltd.
 *
 * History:
 *   13-4-14 下午4:15 Created by dwzhang
 */
package com.jade.framework.base.criterion.expression;

import com.jade.framework.base.criterion.Criterion;

/**
 * Superclass of binary logical expressions.
 *
 * @author dwzhang
 * @version 1.0 13-4-14 下午4:15
 */
public abstract class LogicalExpression implements Criterion
{
    private Criterion[] values;
    private String operation;

    protected LogicalExpression(String operation, Criterion... values)
    {
        if (values.length < 2) {
            throw new IllegalArgumentException("invalid argument length:" + values.length);
        }
        this.values = values;
        this.operation = operation;
    }

    public String getOperation()
    {
        return operation;
    }

    public Criterion[] getValues()
    {
        return values;
    }

    @Override
    public String toString()
    {
        StringBuilder sb = new StringBuilder();
        boolean first = true;
        for (Criterion c : values) {
            if (!first) {
                sb.append(" ").append(operation).append(" ");
            }
            sb.append("(").append(c.toString()).append(")");
            first = false;
        }
        return sb.toString();
    }
}
